using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class OrderDetailServiceBase : Service
    {
		public OrderDetailServiceBase(IEntitiesDB entitiesDB) : base(entitiesDB) { }
		
        public virtual Int32 Add(OrderDetail orderDetail)
        {
            orderDetail.Order = EntitiesDB.EntryWithState(orderDetail.Order, EntityState.Unchanged);
            orderDetail.ProductDetail = EntitiesDB.EntryWithState(orderDetail.ProductDetail, EntityState.Unchanged);
            EntitiesDB.OrderDetailSet.Add(orderDetail);
            EntitiesDB.SaveChanges();
            return orderDetail.Id;
        }

        public virtual void Change(OrderDetail orderDetail)
        {
            orderDetail.Order = EntitiesDB.EntryWithState(orderDetail.Order, EntityState.Unchanged);
            orderDetail.ProductDetail = EntitiesDB.EntryWithState(orderDetail.ProductDetail, EntityState.Unchanged);
		    EntitiesDB.Entry(orderDetail).State = EntityState.Modified;

            EntitiesDB.SaveChanges();
        }
		
		public virtual void Remove(int id)
        {
			OrderDetail orderDetail = Find(id);
            try
            {
                EntitiesDB.OrderDetailSet.Remove(orderDetail);
                EntitiesDB.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new RemoveException(string.Format(Resources.RemoveException, orderDetail.ToString()));
            }
        }

        public virtual void Remove(OrderDetail orderDetail)
        {
			Remove(orderDetail.Id);
        }        

        public virtual OrderDetail Find(int id)
        {
			OrderDetail orderDetail = 
				EntitiesDB.OrderDetailSet
					.Include("Order")
					.Include("ProductDetail")
					.FirstOrDefault(x => x.Id == id);
			
			if(orderDetail!=null)
			{
				var orderAux = orderDetail.Order; //EF4.1 bug hack
				var productDetailAux = orderDetail.ProductDetail; //EF4.1 bug hack
			}
			return orderDetail;
        }

        public virtual List<OrderDetail> FindAll()
        {
            return EntitiesDB.OrderDetailSet.OrderBy(x => x.Id).ToList();
        }
		
		public virtual List<OrderDetail> Search(string searchValue)
        {
            return Search(searchValue, string.Empty, true, 0, 0);
        }

        public virtual List<OrderDetail> Search(string searchValue, int pageIndex, int pageSize)
        {
            return Search(searchValue, string.Empty, true, pageIndex, pageSize);
        }

        public virtual List<OrderDetail> Search(string searchValue, string orderBy)
        {
            return Search(searchValue, orderBy, true, 0, 0);
        }

        public virtual List<OrderDetail> Search(string searchValue, string orderBy, int pageIndex, int pageSize)
        {
            return Search(searchValue, orderBy, true, pageIndex, pageSize);
        }

        public virtual List<OrderDetail> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<OrderDetail> query = EntitiesDB.OrderDetailSet;
			
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
		
		public virtual List<OrderDetail> Search(Expression<Func<OrderDetail, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize=10)
        {
            IQueryable<OrderDetail> query = EntitiesDB.OrderDetailSet.Where(searchCriteria);
            
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int SearchCount(string searchValue)
        {
            return EntitiesDB.OrderDetailSet.Count();
        }
		
		public virtual List<OrderDetail> FindAvailableOrderDetailList<TKey>(List<OrderDetail> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<OrderDetail, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<OrderDetail> query = EntitiesDB.OrderDetailSet.Where(a => !notAvailableIDList.Any(id => id == a.Id));

			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int FindAvailableOrderDetailListCount(List<OrderDetail> notAvailableList, string searchValue)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            return EntitiesDB.OrderDetailSet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Count();
        }
	
    }
}
